﻿using System;
using System.ComponentModel;
using Dgg.LearnYourTools.NUnit.C_Assertions.Support;
using NUnit.Framework;

namespace Dgg.LearnYourTools.NUnit.C_Assertions.f_CustomConstraints
{
	[TestFixture]
	public class xxx_GettingSlightlySmarterWithEvents
	{
		[Test]
		public void AssertOnEvents_PlayingSmart()
		{
			var subject = new EventRaisingSubject();

			aPropertyRaisesEventWithName(subject, s => s.I = 3, "I");
			aPropertyRaisesEventWithName(subject, s => s.I = 4, "I");
			aPropertyRaisesEventWithName(subject, s => s.S = "3", "S");
			aPropertyRaisesEventWithName(subject, s => s.S = "4", "S");
		}

		private static void aPropertyRaisesEventWithName(EventRaisingSubject subject, Action<EventRaisingSubject> propertySetting, string expectedPropertyName)
		{
			PropertyChangingEventHandler handler = (sender, e) =>
			{
				Assert.That(sender, Is.SameAs(subject));
				Assert.That(e.PropertyName, Is.EqualTo(expectedPropertyName));
			};

			try
			{
				subject.PropertyChanging += handler;
				propertySetting(subject);
			}
			finally
			{
				subject.PropertyChanging -= handler;
			}
		}
	}
}
